package com.xafc.demo.framework.result;

import lombok.Data;

import java.io.Serializable;

@Data
public class ApiResult<T> implements Serializable {

    private Integer code;
    private String msg;
    private T data;

    public static <T> ApiResult<T> success() {
        final ApiResult<T> result = new ApiResult<>();
        result.setCode(200);
        return result;
    }

    public static <T> ApiResult<T> success(T o) {
        final ApiResult<T> result = success();
        result.setData(o);
        return result;
    }

    public static <T> ApiResult<T> success(Integer code, T o) {
        final ApiResult<T> result = success();
        result.setData(o);
        result.setCode(code);
        return result;
    }

    public static <T> ApiResult<T> error(String msg) {
        final ApiResult<T> result = new ApiResult<>();
        if (msg == null || msg.equals(""))  msg = "服务器端发生错误";
        result.setCode(500);
        result.setMsg(msg);
        return result;
    }

    public static <T> ApiResult<T> unauthorized(String msg) {
        final ApiResult<T> result = new ApiResult<>();
        if (msg == null || msg.equals(""))  msg = "请先登录";
        result.setCode(401);
        result.setMsg(msg);
        return result;
    }

    public static <T> ApiResult<T> deny(String msg) {
        final ApiResult<T> result = new ApiResult<>();
        if (msg == null || msg.equals(""))  msg = "拒绝访问";
        result.setCode(403);
        result.setMsg(msg);
        return result;
    }

    public static <T> ApiResult<T> fail(String msg) {
        final ApiResult<T> result = new ApiResult<>();
        if (msg == null || msg.equals(""))  msg = "输入错误";
        result.setCode(400);
        result.setMsg(msg);
        return result;
    }

    public static <T> ApiResult<T> fail(Integer code, String msg) {
        final ApiResult<T> result = new ApiResult<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> ApiResult<T> value(ResultCode resultCode) {
        final ApiResult<T> result = new ApiResult<>();
        result.setCode(resultCode.getCode());
        result.setMsg(resultCode.getMessage());
        return result;
    }

}
